//
//  selectCityView.swift
//  旅航ios
//
//  Created by tulip on 2021/5/6.
//

import UIKit

class SelectCityView: UIView,UIGestureRecognizerDelegate, MSSCalendarViewControllerDelegate {
   
    var getNetwork:((Dictionary<String,Any>,String,String)->Void)?
    var address1Code:String!
    var address2Code:String!
    var address1Str=""
    var address2Str=""
    var timeBtn:UIButton!
    var address1:UILabel!
    var address2:UILabel!
    var timeStr=""
    var img:UIImageView!
    
    var calenArr:Array<Any> = []
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor=UIColor.black.withAlphaComponent(0.6)

        let box=UIView.init(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 250+StatusBarHeight))
        box.backgroundColor=UIColor.white
        self.addSubview(box)
        
        let closeBtn=UIButton.init(frame: CGRect(x: 0, y: StatusBarHeight+13, width: 39, height: 17))
        closeBtn.setImage(UIImage(named: "back-1"), for: .normal)
        closeBtn.addTarget(self, action: #selector(close), for: .touchUpInside)
        box.addSubview(closeBtn)
        
        let title=UILabel.init(frame: CGRect(x: ScreenWidth/2.0-100, y: StatusBarHeight+13, width: 200, height: 17))
        title.text="修改城市"
        title.textAlignment = .center
        title.textColor=UIColor.init(hexString: "修改城市")
        title.font=UIFont.systemFont(ofSize: 18)
        box.addSubview(title)
        
        address1=UILabel.init(frame: CGRect(x: 18, y: TopSpaceHeight+19, width: 100, height: 21))
        address1.textColor=UIColor.init(hexString: "333333")
        address1.font=UIFont.systemFont(ofSize: 21)
        address1.text=address1Str
        address1.isUserInteractionEnabled=true
        box.addSubview(address1)
        let tap_c1=UITapGestureRecognizer.init()
        tap_c1.addTarget(self, action: #selector(chooseAddress1))
        address1.addGestureRecognizer(tap_c1)
        
        img=UIImageView.init(frame: CGRect(x: ScreenWidth/2.0-17.5, y: TopSpaceHeight+12, width: 35, height: 35))
        img.image=UIImage(named: "往返切换-飞机")
        img.isUserInteractionEnabled=true
        box.addSubview(img)
        
        let tap=UITapGestureRecognizer.init()
        tap.addTarget(self, action: #selector(changeAddress))
        img.addGestureRecognizer(tap)
        
         address2=UILabel.init(frame: CGRect(x: SCREEN_WIDTH - 118, y: TopSpaceHeight+19, width: 100, height: 21))
        address2.textColor=UIColor.init(hexString: "333333")
        address2.font=UIFont.systemFont(ofSize: 21)
        address2.text=address2Str
        address2.isUserInteractionEnabled=true

        address2.textAlignment = .right
        box.addSubview(address2)
        let tap_c2=UITapGestureRecognizer.init()
        tap_c2.addTarget(self, action: #selector(chooseAddress2))
        address2.addGestureRecognizer(tap_c2)
        
        let line=UIView.init(frame: CGRect(x: 15, y: TopSpaceHeight+58, width: ScreenWidth-30, height: 1))
        line.backgroundColor=UIColor.init(hexString: "ebebeb")
        box.addSubview(line)
        
        timeBtn=UIButton.init(frame: CGRect(x: 18, y: TopSpaceHeight+81, width: 200, height: 18))
        timeBtn.contentHorizontalAlignment = .left
        timeBtn.setTitle(timeStr, for: .normal)
        timeBtn.titleLabel?.font=UIFont.boldSystemFont(ofSize: 18)
        timeBtn.setTitleColor(UIColor.init(hexString: "333333"), for: .normal)
        timeBtn.addTarget(self, action: #selector(chooseTime), for: .touchUpInside)
        box.addSubview(timeBtn)
        
        let line2=UIView.init(frame: CGRect(x: 15, y: TopSpaceHeight+123, width: ScreenWidth-30, height: 1))
        line2.backgroundColor=UIColor.init(hexString: "ebebeb")
        box.addSubview(line2)
        
        let searchBtn=UIButton.init(frame: CGRect(x: 15, y: TopSpaceHeight+142, width: SCREEN_WIDTH-30, height: 45))
        searchBtn.backgroundColor=UIColor.init(hexString: "65a0ff")
        searchBtn.setTitle("搜索", for: .normal)
        searchBtn.setTitleColor(UIColor.white, for: .normal)
        searchBtn.addTarget(self, action: #selector(search), for: .touchUpInside)
        box.addSubview(searchBtn)
        
        
//        let dic = ["departDate":go_Time ?? "","departCityCode":departCityCode ,"arriveCityCode":arriveCityCode]
//
//        Network.request(.searchPriceByday(dic)) { (JSON) in
//            print("\(JSON)")
//            if JSON["code"].int == 1 {
//                guard let arr = JSONDeserializer<PriceCalendarModel>.deserializeModelArrayFrom(json: "\(JSON["data"])")else {
//                    return
//                }
//
//                for model in arr{
//
//                    self.calenArr.append(model as Any)
//                }
//
//
//
//                self.showCalendar()
//            }
//        } failure: { (err) in
//
//        }

        
        //城市选择监听
        NotificationCenter.default.addObserver(self, selector: #selector(chooseDepartCity), name: NSNotification.Name(rawValue: "chooseDepart"), object: nil)
        
        NotificationCenter.default.addObserver(self, selector: #selector(chooseArriveCity), name: NSNotification.Name(rawValue: "chooseArrive"), object: nil)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func close(){
        self.removeFromSuperview()
    }
    
    func setLabText(){
        address1.text=address1Str
        address2.text=address2Str
        timeBtn.setTitle(timeStr, for: .normal)

    }
    
    @objc func changeAddress(){
        transformAnimation(view: img)
        
        let address=address1.text
        address1.text=address2.text
        address2.text=address
        
        let code=address1Code
        address1Code=address2Code
        address2Code=code
        
    }
    
    @objc func chooseAddress1(){
        let vc=SDCitySelectVC.init()
        getCurrentVc().present(vc, animated: true, completion: nil)
    }
    
    @objc func chooseAddress2(){
        let vc=SDCitySelectVC.init()
        vc.isDepart=false
        getCurrentVc().present(vc, animated: true, completion: nil)
    }
    
    @objc func chooseTime(){
        
        
        let vc=MSSCalendarViewController.init()
        vc.limitMonth = 2 ;// 显示几个月的日历
        vc.isDanCheng=true
        vc.type = MSSCalendarViewControllerType.nextType
        vc.beforeTodayCanTouch = false;// 今天之前的日期是否可以点击
        vc.afterTodayCanTouch = false;// 今天之后的日期是否可以点击
        vc.delegate = self
        vc.showPrice = true
       
         vc.priceArr = calenArr
        getCurrentVc().present(vc, animated: true, completion: nil)

    }
    
    
    
    
    
    @objc func search(){
        let sendDic=["departDate":timeStr ,"departCityCode":address1Code ?? "","arriveCityCode":address2Code ?? ""]
        
        if getNetwork != nil {
            getNetwork!(sendDic,address1.text ?? "",address2.text ?? "")
        }
        self.removeFromSuperview()
    }
    
    @objc func chooseDepartCity(sender:Notification){
        let dic=sender.object  as! Dictionary<String, Any>
        address1.text=dic["cityName"] as? String
        address1Code=dic["cityCode"] as? String ?? ""
    }
    
    @objc func chooseArriveCity(sender:Notification){
        let dic=sender.object  as! Dictionary<String, Any>
        address2.text=dic["cityName"] as? String
        address2Code=dic["cityCode"] as? String ?? ""

    }
    
    func calendarViewConfirmClick(withStartDate startDate: Int, endDate: Int) {
        let mdateFormatter=DateFormatter.init()
        mdateFormatter.dateFormat="YYYY-MM-dd"
        timeStr=mdateFormatter.string(from: Date.init(timeIntervalSince1970: TimeInterval(startDate)))
       
        
        let dateFormatter=DateFormatter.init()
        dateFormatter.dateFormat="MM月dd日"
        let mtimeStr = dateFormatter.string(from: Date.init(timeIntervalSince1970: TimeInterval(startDate)))
        timeBtn.setTitle(mtimeStr, for: .normal)

        
    }
    
}
